package com.anpxd.erpad

import android.content.Context
import android.support.multidex.MultiDex
import android.support.multidex.MultiDexApplication
import android.text.TextUtils
import android.util.Log
import com.anpxd.erpad.bean.Shop
import com.anpxd.erpad.bean.User
import com.anpxd.erpad.net.ApiFactory
import com.anpxd.ewalker.utils.GsonUtil
import com.anpxd.framelibrary.net.widget.OkHttpFactory
import com.anpxd.framelibrary.utils.DelegatesExt
import com.gg.baseproject.Response
import com.gg.baseproject.utils.AppConstant
import com.gg.utils.Utils
import com.google.gson.reflect.TypeToken
import com.lsxiao.apollo.core.Apollo
import com.tencent.bugly.Bugly
import com.tencent.bugly.beta.Beta
import io.reactivex.android.schedulers.AndroidSchedulers
import okhttp3.Interceptor
import okhttp3.ResponseBody


/**
 *  Creator : GG
 *  Time    : 2017/10/16
 *  Mail    : gg.jin.yu@gmail.com
 *  Explain :
 */
class App : MultiDexApplication() {

    companion object {
        lateinit var instance: App

    }

    init {
        instance = this

    }

    private var userInfo: String by DelegatesExt.preference(this, AppConstant.USER, "")

    private var shopInfo: String by DelegatesExt.preference(this, AppConstant.SHOP, "")

    //add by xy
    var userName: String = ""
    var passsWord: String = ""
    private val beta: Boolean by lazy { packageName.contains("beta") }
    override fun onCreate() {
        super.onCreate()
        Apollo.init(AndroidSchedulers.mainThread(), this, true)

        initNet()
        if (applicationInfo.packageName == Utils.getCurProcessName(applicationContext)) {
            if (Utils.isApkInDebug(this)) {

            }
            Bugly.init(applicationContext, "029ccd3978", beta)
            Beta.autoInit = true
            Beta.initDelay = 5 * 1000;
            Beta.autoCheckUpgrade = true
            Beta.autoDownloadOnWifi = true
        }
    }

    private fun initNet() {
        ApiFactory.okHttpClient = OkHttpFactory.create(Interceptor {
            val originRequest = it.request()
            // 添加 header 以及公共的 GET 参数
            val newRequest = originRequest.newBuilder()
                    .url(originRequest.url().newBuilder()
//                            .addQueryParameter("uid", LocalUser.userToken?.user_id?:"unlogin")
//                            .addQueryParameter("token", LocalUser.userToken?.token?:"")
//                            .addQueryParameter("device_id", DeviceUtils.getAndroidID())
//                        .addQueryParameter("src", "android")
                            .build()
                    ).build()

            /** 处理不规范的返回值
             *  <-- 400 Bad Request
             *  {
             *      "code": 2,
             *      "msg": "密码错误",
             *      "result": []             // 应该返回 空对象{}, 否则 Json 解析异常
             *  }
             */
            val response = it.proceed(newRequest)
            response.newBuilder()
                    .apply {
                        val originBody = response.body()
                        var json = originBody?.string()

                        var res: Response<Any>? = null

                        try {
                            res = GsonUtil.fromJson(json.toString(), object : TypeToken<Response<Any>>() {}.type)
                        } catch (e: Exception) {
                            Log.e("initNet", "interceptor response" + e)
                        }
                        try {
                            res = GsonUtil.fromJson(json.toString(), object : TypeToken<Response<List<Any>>>() {}.type)
                        } catch (e: Exception) {
                            Log.e("initNet", "interceptor response" + e)
                        }

                        // 不成功，则移除 "result" 字段
                        if (1 != res?.code) {
                            res?.result = null

                        }
                        json = GsonUtil.toJson(res!!)
                        this.body(ResponseBody.create(originBody?.contentType(), json))
                    }
                    .apply {
                        this.code(if (response.code() in 400..500) 200 else response.code())
                    }
                    .build()
        })
    }

    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        MultiDex.install(this)
    }

    fun getUser(): User? = if (TextUtils.isEmpty(userInfo)) null else GsonUtil.jsonToBean(userInfo, User::class.java)!!

    fun setUser(user: User) {
        userInfo = GsonUtil.toJson(user)
    }

    fun setUser(userInfo: String) {
        this.userInfo = userInfo
    }

    fun setShop(shop: Shop) {
        shopInfo = GsonUtil.toJson(shop)
    }

    fun getShop() = if (TextUtils.isEmpty(shopInfo)) null else GsonUtil.jsonToBean(shopInfo, Shop::class.java)!!

    fun getErpUrl(): String = getString(R.string.erp_base_url) ?: ""

    fun getWxUrl() = getString(R.string.wx_base_url) ?: ""

    fun getImageUrl() = getString(R.string.image_url) ?: ""

}